{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "persistent-fundamentals",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "constitutional-relevance",
   "metadata": {},
   "outputs": [],
   "source": [
    "binary = \"11111111010011110100\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "nonprofit-renewal",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['0', '0']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = re.findall(r\"1(0+)1\", binary)\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "coral-denial",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accomplished-steel",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "acoustic-fundamentals",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['101', '1001', '101']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "\n",
    "def findall_overlapped(r, s):\n",
    "    res = []                     # Resulting list\n",
    "    reg = r'^{}$'.format(r)      # Regex must match full string\n",
    "    for q in range(len(s)):      # Iterate over all chars in a string\n",
    "        for w in range(q,len(s)):  # Iterate over the rest of the chars to the right\n",
    "            cur = s[q:w+1]         # Currently tested slice\n",
    "            if re.match(reg, cur): # If there is a full slice match\n",
    "                res.append(cur)    # Append it to the resulting list\n",
    "    return res\n",
    "\n",
    "rex = r'1(0+)1'\n",
    "print(findall_overlapped(rex, binary))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "nervous-representative",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efficient-monthly",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "incorrect-syndrome",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "extreme-vacuum",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/binary-gap/\n",
    "\n",
    "\n",
    "Runtime: 52 ms, faster than 5.76% of Python3 online submissions for Binary Gap.\n",
    "Memory Usage: 14.3 MB, less than 43.90% of Python3 online submissions for Binary Gap.\n",
    "\n",
    "\n",
    "\n",
    "```python\n",
    "import re\n",
    "\n",
    "\n",
    "def findall_overlapped(r, s):\n",
    "    res = []                     # Resulting list\n",
    "    reg = r'^{}$'.format(r)      # Regex must match full string\n",
    "    for q in range(len(s)):      # Iterate over all chars in a string\n",
    "        for w in range(q,len(s)):  # Iterate over the rest of the chars to the right\n",
    "            cur = s[q:w+1]         # Currently tested slice\n",
    "            if re.match(reg, cur): # If there is a full slice match\n",
    "                res.append(cur)    # Append it to the resulting list\n",
    "    return res\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def binaryGap(self, n: int) -> int:\n",
    "        #6:36\n",
    "        binary = bin(n)[2:]\n",
    "        #print(binary)\n",
    "        #results = re.findall(r\"1(0+)1\", binary) # this won't find overlapping match\n",
    "        results = findall_overlapped(r'1(0+)1', binary)\n",
    "        if len(results):\n",
    "            results.sort(key=len)\n",
    "            longest = len(results[-1]) - 2 + 1\n",
    "            return longest\n",
    "        else:\n",
    "            if \"11\" in binary:\n",
    "                return 1\n",
    "            else:\n",
    "                return 0\n",
    "        #6:40\n",
    "        #debug regex until 7:15; findall has flaw, it won't find overlapping match\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "reflected-conducting",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
